package indi.caibo.service_edu.controller;


import indi.caibo.commonutils.JwtUtils;
import indi.caibo.commonutils.R;
import indi.caibo.service_edu.service.EduCourseCollectService;
import indi.caibo.service_edu.service.EduCourseLearnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author Bo Cai
 * @since 2021-12-25
 */
@Api(tags = {"课程学习进度管理模块"})
@RestController
@RequestMapping("/edu-service/course-learn")
public class EduCourseLearnController {
    @Autowired
    EduCourseLearnService courseLearnService;

    @ApiOperation(value = "记录课程学习进度")
    @GetMapping("{courseId}/{chapterId}/{videoId}")
    public R learnCourse(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId,
            @ApiParam(name = "chapterId", value = "章节ID", required = true)
            @PathVariable String chapterId,
            @ApiParam(name = "videoId", value = "课程ID", required = true)
            @PathVariable String videoId,
            HttpServletRequest request
    ) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        courseLearnService.learn(memberId, courseId, chapterId, videoId);
        return R.ok().message("课程学习进度记录成功!");
    }

    @ApiOperation(value = "统计当前用户学过所有课程的进度")
    @GetMapping("")
    public R getTotalLearnProgress(
            HttpServletRequest request
    ) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        List<Map<String, Object>> items = courseLearnService.totalLearnProgress(memberId);
        return R.ok().data("items", items);
    }
}

